<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Member as MemberModel;
use app\common\model\MemberGroup as MemberGroupModel;

use app\admin\validate\Member as MemberValidate;
use think\exception\ValidateException;


use think\facade\Request;
use think\facade\View;
use think\facade\Config;

/**
 * 会员管理
 * Class AdminUser
 * @package app\admin\controller
 */

class Member extends Base
{
    /**
     * @return \think\Response
     */
    public function index()
    {
        $member_list = MemberModel::select();
		View::assign('member_list',$member_list);
		return View::fetch('index');
    }

    /**
     * 添加会员
     * @return mixed
     */
    public function add()
    {

		if (Request::isPost()) {

            $data	= Request::only(['avatar', 'username', 'password', 'nickname', 'member_group_id', 'status']);

            try {
				validate(MemberValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			$data['password'] = md5(Config::get('app.password_salt') . $data['password']);
			if (MemberModel::create($data)) {
				$this->success('保存成功',url('admin/member/index'));
			} else {
				$this->error('保存失败');
			}

        }

        $member_group_list = MemberGroupModel::select();

		View::assign('member_group_list',$member_group_list);
		return View::fetch('add');
    }


    /**
     * 编辑会员
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

            $data	= Request::only(['id','avatar', 'username', 'password', 'nickname', 'member_group_id', 'status']);

            try {
				validate(MemberValidate::class)->scene('edit')->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if(!empty($data['password'])){
				$data['password'] = md5(Config::get('app.password_salt') . $data['password']);
			}else{
				unset($data['password']);
			}
			
			if (MemberModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/member/index'));
			} else {
				$this->error('保存失败');
			}


        }


        $member				= MemberModel::find($id);
        $member_group_list	= MemberGroupModel::select();

		View::assign('member',$member);
		View::assign('member_group_list',$member_group_list);
		return View::fetch('edit');
    }

    /**
     * 删除会员
     * @param $id
     */
    public function delete($id)
    {
        if (MemberModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
